RBAcModule   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 84
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A forRoot 0 16 2
A useCache 0 11 1
A onApplicationBootstrap 0 3 1
A setCacheOptions 0 17 5
A forDynamic 0 36 2
1
import { DynamicModule, Global, Module, OnApplicationBootstrap } from '@nestjs/common';
2
import { RbacService } from './services/rbac.service';
3
import { ModuleRef, Reflector } from '@nestjs/core';
4
import { StorageRbacService } from './services/storage.rbac.service';
5
import { IStorageRbac } from './interfaces/storage.rbac.interface';
6
import { IDynamicStorageRbac } from './interfaces/dynamic.storage.rbac.interface';
7
import { ICacheRBAC } from './interfaces/cache.rbac.interface';
8
import { Ctr } from './ctr/ctr';
9
10
@Global()
11
@Module({
12
    providers: [
13
        RbacService,
14
        StorageRbacService,
15
        Reflector,
16
    ],
17
    imports: [],
18
    exports: [
19
        RbacService,
20
    ],
21
})
22
export class RBAcModule implements OnApplicationBootstrap {
23
    private static cache?: any | ICacheRBAC;
24
    private static cacheOptions?: { KEY?: string, TTL?: number };
25
26
    constructor(
27
        private readonly moduleRef: ModuleRef,
28
    ) {}
29
30
    static useCache(
31
        cache: any | ICacheRBAC,
32
        options?: {
33
            KEY?: string,
34
            TTL?: number
35
        },
36
    ) {
37
        RBAcModule.cache = cache;
38
        RBAcModule.cacheOptions = options;
39
        return RBAcModule;
40
    }
41
42
    static forRoot(
43
        rbac: IStorageRbac,
44
        providers?: any[],
45
        imports?: any[],
46
    ): DynamicModule {
47
48
        return RBAcModule.forDynamic(
49
            /* tslint:disable */
50
            class {
51
                async getRbac(): Promise<IStorageRbac> {
52
                    return rbac;
53
                };
54
            },
55
            providers,
56
            imports,
57
        );
58
    }
59
60
    static forDynamic<T extends new (...args: any[]) => IDynamicStorageRbac>(
61
        rbac: T,
62
        providers?: any[],
63
        imports?: any[],
64
    ): DynamicModule {
65
        const inject = [rbac];
66
        const commonProviders = [];
67
        if (RBAcModule.cache) {
68
            commonProviders.push(RBAcModule.cache, {
69
                provide: 'ICacheRBAC',
70
                useFactory: (cache: ICacheRBAC): ICacheRBAC => {
71
                    return RBAcModule.setCacheOptions(cache);
72
                },
73
                inject: [RBAcModule.cache],
74
            });
75
            inject.push(RBAcModule.cache);
76
        }
77
78
        commonProviders.push(...[
79
            ...(providers || []),
80
            rbac,
81
            {
82
                provide: StorageRbacService,
83
                useFactory: async (rbacService: IDynamicStorageRbac, cache?: ICacheRBAC) => {
84
                    return new StorageRbacService(rbacService, RBAcModule.setCacheOptions(cache));
85
                },
86
                inject,
87
            },
88
        ]);
89
90
        return {
91
            module: RBAcModule,
92
            providers: commonProviders,
93
            imports: [
94
                ...(imports || []),
95
            ],
96
        };
97
    }
98
99
    private static setCacheOptions(cache?: ICacheRBAC) {
100
        if (!cache || RBAcModule.cacheOptions) {
101
            return cache;
102
        }
103
        if (!RBAcModule.cacheOptions) {
104
            return cache;
105
        }
106
        if (RBAcModule.cacheOptions.KEY) {
107
            cache.KEY = RBAcModule.cacheOptions.KEY;
108
        }
109
110
        if (RBAcModule.cacheOptions.TTL) {
111
            cache.TTL = RBAcModule.cacheOptions.TTL;
112
        }
113
114
        return cache;
115
    }
116
117
    onApplicationBootstrap(): any {
118
        Ctr.ctr = this.moduleRef
119
    }
120
}
121